<?php
/*
 * Gravy's DateHelper Class
 */
 class datehelper extends errorHandler{
	 function __construct(){
	 }

	 /**
	 *
	 * Check if the string is a valid date with strtotime
	 * If not, modify the / to - (or - to /) and try again
	 * @param string $str
	 * @return string or false
	 */
	static function validate($str){
		if( ! strtotime($str)){
			$str = str_replace('/', '-', $str);
			if( ! strtotime($str)){
				$str = str_replace('-', '/', $str);
				if( ! strtotime($str)){
					return false;
				}else{
					return $str;
				}
			}else{
				return $str;
			}
		}else{
			return $str;
		}
	}

	 /**
	  * Get a Month's Beginning Timestamp
	  * Returns the supplied month's beginning date in timestamp format.
	  * @param month
	  * @param year
	  * @return double
	  */
	 static function getMonthTimestampStart($month_id=NULL, $year_id=NULL){
		$result=false;
		$month_id=($month_id===NULL)?date("n"):$month_id;
		$year_id=($year_id===NULL)?date("Y"):$year_id;

		$result=strtotime("$year_id-$month_id-1");
		return($result);
	 }

	static function workingDays($datecalc,$duedays) {
		$i = 1;
		while($i <= $duedays) {
			$date_info=getdate($datecalc);
			if (($date_info["wday"] == 0) or ($date_info["wday"] == 6) )  {
				$datecalc += 86400; // Add a day.
				continue;
			}
			$datecalc += 86400; // Add a day.
			$i++;
		}
		return($datecalc);
	}

	 /**
	  * Get a Month's Ending Timestamp
	  * Returns the supplied month's end date in timestamp format.
	  * @param month
	  * @param year
	  * @return double
	  */
	 static function getMonthTimestampEnd($month_id=NULL, $year_id=NULL){
		$result=false;
		$month_id=($month_id===NULL)?date("n"):$month_id;
		$year_id=($year_id===NULL)?date("Y"):$year_id;

		$nextMonth=($month_id+1>12)?1:($month_id+1);
		$nextYear=($month_id+1>12)?$year_id+1:$year_id;
		$result=(strtotime($nextYear."-".$nextMonth."-1")-1);
		return($result);
	 }

	 /**
	  * Get a duration's timestamp
	  * Returns the timestamp of a given duration (time), in the format of {{{h}h:}{{{m}m:}{{s}s}}}
	  * @param string duration
	  */
	 static function getDurationTimestamp($duration=NULL){
		$result=false;
		$duration=trim($duration);
		$duration=preg_replace("/[^\d:\s\.]/","", $duration);
		if(strpos($duration,":")>-1||strpos($duration," ")>-1||strpos($duration,".")>-1){
			$ar=preg_split("/[:\s\.]/",$duration);
			if(count($ar)==1){
				$result=(int)$ar[0];
			}elseif(count($ar)==2){
				$result=((int)$ar[0]*60);
				$result+=(int)$ar[1];
			}elseif(count($ar)==3){
				$result=((int)$ar[0]*3600);
				$result+=((int)$ar[1]*60);
				$result+=(int)$ar[2];
			}elseif(count($ar)==4){
				$result=((int)$ar[0]*86400);
				$result+=((int)$ar[1]*3600);
				$result+=((int)$ar[2]*60);
				$result+=(int)$ar[3];
			}
		}else{
			$result=(int)$duration;
		}
		return($result);
	 }

	 /**
	  * Seconds to Time
	  * Returns the string representation of time given the number of seconds.
	  * @param int seconds
	  */
	 static function getSecondsToTime($seconds=NULL){
		$result=false;
		$seconds=(int)$seconds;
		if($seconds>59){
			$tmp=$seconds;
			$hours=floor($tmp/3600);
			$tmp-=($hours*3600);
			$mins=floor($tmp/60);
			$tmp-=($mins*60);
			$secs=$tmp;
			$result="";
			if($hours>0){
				$result.=$hours.":";
			}
			if($mins>0){
				$result.=substr("0".$mins,-2).":";
			}elseif(strlen($result)>0){
				$result.="00:";
			}
			if($secs>0){
				$result.=substr("0".$secs,-2);
			}elseif(strlen($result)>0){
				$result.="00";
			}
		}else{
			$result=$seconds;
		}
		return($result);
	 }

	 /**
	  * Date Difference Function
	  * Returns the difference of two dates.
	  * The data-type of the return value varies depending on the option set in $options.
	  */
	 public function difference($date1=NULL,$date2=NULL,$options=NULL){
		 $result="";
		 $centuries = 0;
		 $decades = 0;
		 $years = 0;
		 $semesters = 0;
		 $quarters = 0;
		 $months = 0;
		 $fortnights = 0;
		 $weeks = 0;
		 $days = 0;
		 $hours = 0;
		 $minutes = 0;
		 $seconds = 0;
		 $date1=($date1==NULL)?time():$date1;
		 $date2=($date2==NULL)?time():$date2;
		 if(!is_numeric($date1)){
			 $date1=strtotime($date1);
		 }
		 if(!is_numeric($date2)){
			 $date2=strtotime($date2);
		 }

		 $options=($options==NULL)?"y, m, d, h, n, s":$date2;

		 $diff=$date1-$date2;

		 $year_in_seconds=(60*60*24*365);
		 $years=floor($diff/$year_in_seconds);
		 $diff-=($years*$year_in_seconds);

		 $month_in_seconds=(60*60*24*30);
		 $months=floor($diff/$month_in_seconds);
		 $diff-=($months*$month_in_seconds);

		 $days_in_seconds=(60*60*24);
		 $days=floor($diff/$days_in_seconds);
		 $diff-=($days*$days_in_seconds);

		 if($years>0){
			 $suffix=($years>1)?"s":"";
			 $result="$years year$suffix";
		 }
		 if($months>0){
			 $suffix=($months>1)?"s":"";
			 $result.=(strlen($result)>0)?", ":"";
			 $result.="$months month$suffix";
		 }
		 if($days>0){
			 $suffix=($days>1)?"s":"";
			 $result.=(strlen($result)>0)?", ":"";
			 $result.="$days day$suffix";
		 }

		 return($result);
	 }
 }
?>
